#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
using namespace std;
#define NUM 10
//int main()
//{
//  pid_t id=fork();
//  if(id==0)
//  {
//
//  
//    printf("开始执行\n");
//    //printf("结束进程\n");
//    execl("use/bin/ls","ls","-a","-l,NULL");
//    printf("结束进程\n");
//  }
//  //printf("父进程\n");
//  return 0;
//}


//int main()
//{
//  printf("Hello word\n");
//  execl("/usr/bin/ls","ls","-a","-l",NULL);
//  printf("over come\n");
//  return 0;
//}

int main(int argc, char*argv[], char *env[])
{
  pid_t id=fork();
  if(id==0)
  {
    char* const _argv[NUM]={
   (char*)"ls",(char*)"-l",(char*)"-a",NULL
    };
    printf("Hello Linux\n");
    //子进程
    //execl("/usr/bin/ls","ls","-a","-l",NULL);//execl使用
    //execlp("ls","ls","-a","-l",NULL);
    //execvp("ls",_argv);
    //execv("/usr/bin/ls", _argv); //和上面的execl只有传参方式的区别
    //execle("/usr/bin/ls","ls","-a","-l",NULL,_env);

  }
  //sleep(10);
   //父进程
  else
   {
       printf("父进程开始运行, pid: %d\n", getpid());
       int status = 0;
       id_t id = waitpid(-1, &status, 0); //阻塞等待, 一定是子进程先运行完毕，然后父进程获取之后，才退出！
       if(id > 0)
          printf("wait success, exit code: %d\n", WEXITSTATUS(status));
       
   }
  return 0;
}
